<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>生产</title>
    <script src="../lib/jquery-1.11.3.js"></script>
    
    <script src="../lib/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>
    <link
      rel="stylesheet"
      href="../lib/bootstrap-3.3.5-dist/css/bootstrap.min.css"
    />
   
    <link rel="stylesheet" href="../css/plant.css" />
    <link rel="stylesheet" href="../css/index.css" />
    <link rel="stylesheet" href="../font/font.css" />
  </head>
<style>
    #paging div:last-child div:nth-child(2) {
      opacity: 1;
      border-radius: 4px;
      font-size: 14px;
      background: rgba(255, 255, 255, 0.39);
      border: 1px solid #999999;
      color: #999;
    }
    #page span {
      border: none;
      display: inline-block;
      width: 36px;
      height: 36px;
      cursor: pointer;
    }
    .active {
      background: #4dc3b6;

      border-radius: 4px;
      border: none;
      color: #fff;
    }
    #pre,
    #next {
      cursor: pointer;
    }
  </style>
  <body>
     <!-- 增加的模态框 -->
    <div
      class="modal fade"
      id="myModal"
      tabindex="-1"
      role="dialog"
      aria-labelledby="myModalLabel"
    >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">新增</h4>
          </div>
          <div class="modal-body">
            <form>
               <div class="form-group">
                <label for="recipient-name" class="s_name">来源仓库</label>
                <select name="" id="house_name">
                  <option value="100001">鑫巨龙</option>
                  <option value="100002">艾正</option>
                  <option value="100003">禾领</option>
                  <option value="100004">安腾</option>
                  <option value="100005">神飞亿</option>
                  <option value="100006">熙衡</option>
                  <option value="100007">枫华</option>
                  <option value="100008">豪益</option>
                  <option value="100009">平康</option>
                  <option value="100010">天海通达</option>
                  
                  
                </select>
              </div>
              <div class="form-group">
                <label for="recipient-name" class="s_name">产品名称</label>
                <select name="" id="c2_name">
                 
                </select>
                
              </div>
              <div class="form-group" >
                <label for="message-text" class="control-label" >出库单号</label>
                <select name="" id="outNumber">

                </select>
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">包装方式</label>
                <input type="text" class="form-control" id="p_package" />
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">生产日期</label>
                <input type="date" class="form-control" id="p_date" />
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">批次号（生产日期+当天第几批）</label>
                <input type="text" class="form-control" id="p_number"></input>
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">规格（单位：克）</label>
                <input type="text" class="form-control" id="p_specifications"></input>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              取消
            </button>
            <button
              type="button"
              class="btn btn-primary"
              id="confirm"
              class="confirm"
            >
              确定
            </button>
          </div>
        </div>
      </div>
    </div>
<!-- 修改的模态框 -->
   <div
      class="modal fade"
      id="myModal1"
      tabindex="-1"
      role="dialog"
      aria-labelledby="myModalLabel"
    >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">修改</h4>
          </div>
          <div class="modal-body">
            <form>
              <div class="form-group">
                <label for="message-text" class="control-label">产品名称：</label>
                <span id="p_name1"></span>
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">包装方式</label>
                <input type="text" class="form-control" id="p_package1" />
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">生产日期</label>
                <input type="date" class="form-control" id="p_date1" />
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">批次号（生产日期+当天第几批）</label>
                <input type="text" class="form-control" id="p_number1"></input>
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">规格（单位：克）</label>
                <input type="text" class="form-control" id="p_specifications1"></input>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              取消
            </button>
            <button
              type="button"
              class="btn btn-primary"
              id="confirm1"
              class="confirm"
            >
              确定
            </button>
          </div>
        </div>
      </div>
    </div>

    <div class="container-fluid">
      <div class="row">
        <!-- 这是侧边栏 -->
        <div id="kong" class="col-md-2"></div>
        <div id="main_menu" class="col-md-2">
          <ul id="myul">
            <li id="logoli">
              <div>
                <span class="iconfont icon-shouyefill"></span
                ><a href="../index.html">首页</a>
              </div>
            </li>
            <li id="have">
              <div><span class="iconfont icon-tongji"></span>统计</div>
              <ul>
                <li><a href="../pages/zx_tongji.html">种植收购统计</a></li>
                <li><a href="../pages/saleStatistical.html">销售统计</a></li>
                <li><a href="../pages/dj03.html">人员统计</a></li>
              </ul>
            </li>
            <li>
              <div>
                <span class="iconfont icon-zhongzhiguanli"></span>种植养殖
              </div>
              <ul>
                <li><a href="../pages/plant.html">蔬果</a></li>
                <li><a href="../pages/farming.html">肉类</a></li>
              </ul>
            </li>
            <li>
              <div><span class="iconfont icon-caigou"></span>采购收购</div>
              <ul>
                <li>
                  <a href="../pages/Supplier_management.html">供应商管理</a>
                </li>
                <li><a href="../pages/pick_fruit.html">蔬果</a></li>
                <li><a href="../pages/pick_meat.html">肉类</a></li>
              </ul>
            </li>
            <li>
              <div>
                <span class="iconfont icon-yunshuzhongwuliu-xianxing"></span
                >运输
              </div>
              <ul>
                <li><a href="../pages/tran_meat.html">肉类运输</a></li>
                <li><a href="../pages/tran_ve.html">蔬菜运输</a></li>
              </ul>
            </li>
            <li>
              <div><span class="iconfont icon-xinruku"></span>仓库</div>
              <ul>
                <li><a href="../pages/stockIn.html">入库</a></li>
                <li><a href="../pages/stockOut.html">出库</a></li>
              </ul>
            </li>
            <li>
              <div id="logo">
                <span class="iconfont icon-shejishengchan"></span
                ><a href="../pages/production.html">生产</a>
              </div>
            </li>
            <li>
              <div><span class="iconfont icon-xiaoshou"></span>销售</div>
              <ul>
                <li><a href="../pages/zx_xiaoshou.html">销售流通</a></li>
                <li><a href="../pages/online_sho_by.html">网购</a></li>
                <li>
                  <a href="../pages/online_retreate.html">网购-退换记录</a>
                </li>
                <li><a href="../pages/zx_pima.html">批码管理</a></li>
              </ul>
            </li>
            <li>
              <div>
                <span class="iconfont icon-lajitong"></span
                ><a href="../pages/waste_management.html">废料去向</a>
              </div>
            </li>
            <li>
              <div><span class="iconfont icon-guanliyuan"></span>管理</div>
              <ul id="secondary">
                <li><a href="../pages/dj01.html">人员管理</a></li>
                <li><a href="../pages/dj02.html">操作日志</a></li>
                <li><a href="../pages/board.html">留言板</a></li>
              </ul>
            </li>
          </ul>
        </div>
        <div id="left_content" class="col-md-10">
          <!-- 这是头 -->
          <nav id="main_headnav">
            <div class="clear">
              <div class="navlefspan">
                <span class="iconfont icon-caidan" id="hidenmenu"></span>
                <span class="mytitle">食品流通安全管理系统</span>
              </div>
              <div class="navrigspan">
                <span class="iconfont icon-tixing" id="tx"></span>
                <!-- 头像 -->
                <img src="../images/shouye/tx.jpg" alt="" />
                <!-- 姓名 -->
                <span id="tc">退出</span>
              </div>
            </div>
            <div>
              <!-- 这里写小的头 -->
            </div>
          </nav>
          <div id="main_content">
            <div id="mycentent">
              <!-- 这是内容 -->
              <div id="box">
                <div id="main">
                  <p id="mynav">
                    <span>生产</span>
                    <!-- <span>></span><span class="mycolor2">蔬果</span> -->
                  </p>
                  <div id="add">
                    <span>生产信息</span>
                    <button id="addPro">新增</button>
                  </div>
                  <div id="search" class="row">
                    <span>产品名称</span
                    ><input type="text" placeholder="请输入" id="findName" />
                    <span>包装方式</span
                    ><input type="text" placeholder="请输入" id="findState" />

                    <button id="find">查询</button>
                    <button id="reset">重置</button>
                  </div>
                  <table class="table table-bordered" id="mytable">
                    <thead>
                      <tr>
                        <th>产品名称</th>
                        <th>包装方式</th>
                        <th>生产时间</th>
                        <th>生产批次</th>

                        <th>生产规格(单位：克)</th>

                        <th>操作</th>
                      </tr>
                    </thead>
                    <tbody></tbody>
                  </table>
                </div>
                <div id="paging" class="clear">
                  <div>
                    跳至 <input type="text" placeholder="5" id="jumpPage" /> 页
                  </div>
                  <div>
                    <select name="" id="setPage">
                      <option value="">10条/页</option>
                      <option value="">20条/页</option>
                      <option value="">30条/页</option>
                    </select>
                  </div>
                  <div class="clear" id="page">
                  
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
   
  </body>
  <script src="../js/index.js"></script>
  <script>
    // $("#add button").click(() => {
    //   window.location = "./plant-add.html";
    // });
    window.onload = function () {
      let x = $("#box").width();
      $("#box").width(x);
    };

    let count = 10;
    let page = 1;
    let maxPage;
    let id_val;
    getData();

    function getData() {
      $.ajax({
        url: "/production/get",
        data: {
          count: count,
          page: page,
          c2_name: $("#findName").val(),
          p_package: $("#findState").val(),
        },
        success: (res) => {
          if (res.code == 302) {
            location.href = "/";
            return;
          }
          if (res.error) {
            console.log(res.error);
          } else {
            //列表渲染
            $("tbody").html("");
            $(res.searchDate).each((i, v) => {
              $("tbody").append(`
                            <tr data-id="${v.p_id}">
                                <td>${v.c2_name}</td>
                                <td>${v.p_package}</td>
                                <td>${v.p_date.substring(0, 10)}</td>
                          <td>${v.p_number}</td>
                          <td>${v.p_specifications}</td>
                                <td>
                                    <a class="edit">修改</a>
                                    <a class="del">删除</a>

                                </td>
                            </tr>
                        `);
            });
            //页码渲染

            $("#page").html(`<div id="pre">
                      <p><</p>
                    </div>
                    <div id="next">
                      <p>></p>
                    </div>`);
            maxPage = Math.ceil(res.total / count);
console.log( maxPage)
            for (let i = maxPage; i >= 1; i--) {
              if (page == i) {
                $("#pre").after(`
                <div><span class="active"> ${i}</span></div>

                            `);
              } else {
                $("#pre").after(`
                <div><span> ${i}</span></div>
                 `);
              }
            }
          }
        },
      });
    }

    // 点击页码

    $("#page").on("click", "span", function () {
      page = $(this).text();
      getData();
    });
     // 上一页
    $("#page").on("click", "#pre", function () {
      if (page > 1) {
        page--;
        getData();
      }
    });
    //下一页
    $("#page").on("click", "#next", function () {
      if (page < maxPage) {
        page++;
        getData();
      }
    });
    // 跳转页码

    $("#jumpPage").blur(function () {
      page = $("#jumpPage").val();
      getData();
    });
    // 分页条数设置
    $("#setPage").blur(function () {
      let a = parseInt($("#setPage").find("option:selected").text());
      count = a;
      getData();
    });
    // 查找
    $("#find").click(() => {

      getData();
    });

    //重置
    $("#reset").click(() => {
      page = 1;
      count = 10;
      $("#findName").val("");
      $("#findState ").val("");
      getData();
    });

    //增加
$('#addPro').click(()=>{
  $('#myModal').modal({
  keyboard: false
})
})
    $("#confirm").click(() => {
      let a = $("#c2_name").find("option:selected").val();
      let b = $("#p_package").val();
      let c = $("#p_date").val();
      let d = $("#p_number").val();
      let e = $("#p_specifications").val();
      let f = $("#outNumber").find("option:selected").text();
     $('#myModal').modal('hide')
      $.ajax({
        url: "/production/Add",
        method: "post",
        data: {
          c2_name: a,
          p_package: b,
          p_date: c,
          p_number: d,
         p_specifications: e,
         house_id:f
        },
        success: (res) => {
          // console.log(res);
         alert('添加成功')
          getData();

        },
      });
    });

// 删除
  $("tbody").on("click", ".del", function () {
    if (confirm("确定删除吗？")) {
      $.ajax({
        url: "/production/del",
        method: "post",
        data: {
          id: $(this).parents("tr").attr("data-id"),
        },
        success: (res) => {
          if (res.error) {
            // console.log(res.msg);
            alert('已进入销售，无法删除')
          } else {
            alert("删除成功");
            getData();
          }
        },
      });
    }
    return false;
  });
  //渲染各库房下的商品明细

  
  function getgoods(){
    $.ajax({
        url: "/production/get1",
        data: {
         house_id:$('#house_name').find("option:selected").val()
        },
        success: (res) => {
          if (res.code == 302) {
            location.href = "/";
            return;
          }
          if (res.error) {
            console.log(res.error);
          } else {
            
            //列表渲染
            $("#c2_name").html("");
            $(res.data).each((i, v) => {
              $("#c2_name").append(`
              <option value="">${v.c2_name}</option>`)
              $('#outNumber').html("")
              $('#outNumber').append(`
                <option value="">${v.p_ck_id}</option>
              `)
              
            });
          
          }
        },
      });
  }
  
  $('#house_name').blur(()=>{
    console.log(1);
    getgoods()
  })

$('tbody').on('click','.edit',function(){
    $('#myModal1').modal({
    keyboard: false})
    // 打开模态框时填入原数据
      id_val = $(this).parents("tr").attr("data-id");
      let a = $(this).parents("tr").children().eq(0).text();
      let b = $(this).parents("tr").children().eq(1).text();
      let c = $(this).parents("tr").children().eq(2).text();
      let d = $(this).parents("tr").children().eq(3).text();
      let e = $(this).parents("tr").children().eq(4).text();
console.log(id_val,a,b,c,d,e)
$("#p_name1").html("")
      $("#p_name1").append(a);
      $("#p_package1").val(b);
      $("#p_date1").val(c);
      $("#p_number1").val(d);
      $("#p_specifications1").val(e);
})

 $("#confirm1").click(() => {
      let a = $("#p_name1").text();
      let b = $("#p_package1").val();
      let c = $("#p_date1").val();
      let d = $("#p_number1").val();
      let e = $("#p_specifications1").val();
console.log(a,b,c,d,e)
      $("#myModal1").modal("hide");
      $.ajax({
        url: "/production/edit",
        
        data: {
          id: id_val,
          p_package: b,
          
         p_date: c,
          p_number: d,
          p_specifications: e,
        },
        success: (res) => {
          if (res.error) {
            console.log(res.msg);
          } else {
            alert("修改成功");
            getData();
          }
        },
      });
    });
  </script>

  
</html>
